// index.js
// 获取应用实例
/*MD5密码加密*/
import {
        hexMD5
} from "../../utils/md5.js"

const app = getApp()

Page({
        data: {
                latitude: 23.54835,
                longitude: 113.58646,
                yonghuming: '',
                mima: '',
                backimg: "https://pic.vjshi.com/2019-05-12/efc40dc678b5ffe999bb09311608ccdb/00003.jpg?x-oss-process=style/watermark",
                gif: "https://wimg.588ku.com/gif/20/11/24/c40547e303a9cd4102490e11c94a9b6f.gif",
                mylist: [{
                                "id": "用户名",
                                "type": 'number',
                                "passwd": false,
                                "icon": "https://6d79-mybiyesheji-1g9dugc1e05b04c4-1309023553.tcb.qcloud.la/icon/%E8%B4%A6%E5%8F%B7%E7%AE%A1%E7%90%86.png?sign=87323fdc98522f86a98b83e6de1ebc72&t=1669701783",
                                "plac": "请输入您的用户名",
                                "long": "13",
                                "Tap": 'inputuserName'

                        },
                        {
                                "id": "密码",
                                "type": 'text',
                                "passwd": true,
                                "icon": "https://6d79-mybiyesheji-1g9dugc1e05b04c4-1309023553.tcb.qcloud.la/icon/%E5%AF%86%E7%A0%81.png?sign=ca7eac4d3b316460f54d81afbe1eaa68&t=1669701805",
                                "plac": "请输入您密码信息",
                                "long": "13",
                                "Tap": 'inputpasswd'
                        },
                ],
                button: [{
                                "name": "注册",
                                "Tap": "Enroll"
                        },
                        {
                                "name": "登陆",
                                "Tap": "Login"
                        }
                ],
                donghua1: {},
                donghua0: {},
                images0: {},
                am0: {}
        },
        /*数据库输入测试*/
        // Uodatatodb() {
        //         const db = wx.cloud.database();
        //         const test = db.collection("test")
        //         test.add({
        //                 data: {
        //                         userName: "柯林顿",
        //                         userPhone: "13078322916"
        //                 },
        //                 success: res => {
        //                         console.log(hexMD5(13078322916));
        //                         console.log("插入成功！")
        //                 }
        //         })
        // },
        /*注册事件 */
        Enroll() {
                console.log("点击了注册按钮！");
                wx.vibrateShort({
                        type: 'heavy',
                })
                wx.navigateTo({
                        url: '/pages/zuce/zuce',
                })
        },
        /*登陆事件 */
        Login() {
                var that = this
                let yonghuming = this.data.yonghuming
                let mima = this.data.mima

                // const openid = app.globalData.openid

                // console.log(openid)
                console.log("点击了登陆按钮！");
                const db = wx.cloud.database();
                const test = db.collection("test")
                if (yonghuming != null || mima != null) {
                        test.where({
                                userName: yonghuming
                        }).get({
                                success: function (res) {
                                        console.log(res.data.length)
                                        if (res.data.length == 0) {
                                                wx.showModal({
                                                        title: "账号没有注册",
                                                        content: "是否要跳转到注册页面？",
                                                        showCancel: true,
                                                        cancelText: '取消',
                                                        success(res) {
                                                                if (res.confirm) {
                                                                        console.log('用户点击确定')
                                                                        wx.vibrateShort({
                                                                                type: 'heavy',
                                                                        })
                                                                        wx.navigateTo({
                                                                                url: '/pages/zuce/zuce',
                                                                        })
                                                                } else if (res.cancel) {
                                                                        console.log('用户点击取消')
                                                                        wx.vibrateShort({
                                                                                type: 'haevy',
                                                                        })
                                                                }
                                                        }
                                                })
                                        }
                                        // console.log(res.data[0].mima)
                                        // console.log("查询成功!")
                                        // console.log("密码加密为：",mima)
                                        if (mima == res.data[0].mima) {
                                                wx.navigateTo({
                                                        url: '/pages/index/index?latitude=' + that.data.latitude + "&longitude=" + that.data.longitude
                                                })
                                                /*存储密码进入缓存 */
                                                wx.setStorage({
                                                        key:"myPassWD",
                                                        data:mima
                                                })
                                                wx.setStorage({
                                                        key:"myUserNA",
                                                        data:yonghuming
                                                })
                                                wx.getStorage({
                                                        key:"myPassWD",
                                                        success(res){
                                                                console.log(res)
                                                        }  
                                                })
                                                wx.getStorage({
                                                        key:"myUserNA",
                                                        success(res){
                                                                console.log(res)
                                                        }  
                                                })

                                                wx.vibrateShort({
                                                        type: 'heavy',
                                                })

                                        }
                                        if (mima != res.data[0].mima && mima != '') {
                                                wx.showToast({
                                                        title: '密码错误',
                                                        icon: "error"
                                                })
                                                wx.vibrateShort({
                                                        type: 'heavy',
                                                })
                                        }


                                },

                        })
                }
                if (yonghuming == '' || mima == '') {
                        wx.showToast({
                                title: '信息不完整',
                                icon: "error"
                        })
                        wx.vibrateShort({
                                type: 'heavy',
                        })
                }
        },
        /*账号输入信息 */

        inputuserName: function (e) {
                this.data.yonghuming = e.detail.value
                console.log(this.data.yonghuming)
                wx.vibrateShort({type: 'heavy',
                })
        },
        /*密码输入信息 */
        inputpasswd: function (e) {
                // 进行 4 次的md5加密算法，确保用户密码安全
                this.data.mima = hexMD5(hexMD5(hexMD5(hexMD5(e.detail.value))))
                // console.log(this.data.mima)
                wx.vibrateShort({
                        type: 'heavy',
                })
        },

        onLoad() {
                var that = this
                wx.getLocation({
                        type: "wsg84",
                        success(res) {
                                console.log(res)
                                that.setData({
                                        longitude: res.longitude,
                                        latitude: res.latitude
                                })
                        }
                })

                /**创建动画实例 */
                this.donghua1 = wx.createAnimation({
                        duration: 300,
                        timingFunction: 'ease'
                })
                this.donghua0 = wx.createAnimation({
                        duration: 300,
                        timingFunction: 'ease'
                })
                /***按钮动画 */
                this.am0 = wx.createAnimation({
                        duration: 300,
                        timingFunction: 'ease'
                })
                /**顶部图片的动画 */
                this.images0 = wx.createAnimation({
                        duration: 300,
                        timingFunction: "ease-in"
                })


        },
        onShow() {
                this.images0.translateY(0).step()
                this.images0.translateY(15).step()
                this.images0.translateY(0).step()

                this.donghua1.translateY(0).step()
                this.donghua1.translateY(15).step()
                this.donghua1.translateY(0).step()

                this.donghua0.translateY(0).step()
                this.donghua0.translateY(-15).step()
                this.donghua0.translateY(0).step()

                this.am0.scale(1.5).step()
                this.am0.scale(1.2).step()
                this.setData({
                        donghua1: this.donghua1.export(),
                        donghua0: this.donghua0.export(),
                        images0: this.images0.export(),
                        am0: this.am0.export()
                })
        },
        onReady(){
                var that = this
                /*数据库访问*/
                const db = wx.cloud.database();
                const test = db.collection("test")
                /*获取缓存内的账号和密码 */
                wx.getStorage({
                        key:"myPassWD",
                        success(res){
                               that.setData({
                                        mima : res.data
                                })
                        }
                })
               
                wx.getStorage({
                        key:"myUserNA",
                        success(res){
                                test.where({
                                        userName:res.data
                                }).get({
                                        success:function(res)
                                        {   
                                                console.log(res.data[0].mima)
                                                if(res.data[0].mima == that.data.mima)
                                                {
                                                        wx.navigateTo({
                                                                url: '/pages/index/index?latitude=' + that.data.latitude + "&longitude=" + that.data.longitude
                                                        })
                                                }
                                        }
                                })
                        }
               })
               
               
               
        }

})